﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLHD.DAL;
using QLHD.BLL;
using System.Data.SqlClient;

namespace QLHD
{
    public partial class frmQLKho : Form
    {
        public frmQLKho()  { }
        public frmQLKho(frmMain parent)
        {
            InitializeComponent();
            MdiParent = parent;
        }

        private void frmQLKho_Load(object sender, EventArgs e)
        {
            LoadData();
            int index = 0;
            DisplayData(index);
            ReadonlyTextbox();

        }
        void LoadData()
        {
            try
            {
                DataTable dt = new DataTable();
                dt = Kho_DB.SelectKho();
                dgvKho.DataSource = dt;
            }
            catch (SqlException ex)
            {

                MessageBox.Show("Xảy ra lỗi sau:" + "/n" + ex.ToString());
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void dgvKho_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            DisplayData(index);
          
        }
        void DisplayData(int index)
        {
            try
            {                
                txtMaKho.Text = dgvKho.Rows[index].Cells[0].Value.ToString();
                txtTenKho.Text = dgvKho.Rows[index].Cells[1].Value.ToString();
                txtKhoiLuong.Text = dgvKho.Rows[index].Cells[2].Value.ToString();
                txtDiaChi.Text = dgvKho.Rows[index].Cells[3].Value.ToString();
            }
            catch (Exception)
            {
                return;
            }
        }
        void ReadonlyTextbox()
        {
            txtDiaChi.ReadOnly = true;
            txtMaKho.ReadOnly = true;
            txtTenKho.ReadOnly = true;

        }
        void NotReadonlyTextbox() 
        {
            txtDiaChi.ReadOnly = false;
            txtMaKho.ReadOnly = false;
            txtTenKho.ReadOnly = false;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "Thêm")
            {
                btnThem.Text = "Lưu";
                NotReadonlyTextbox();
                ClearTextbox();
                txtMaKho.Focus();
            }
            else
            {
                try
                {
                    Kho newKho = new Kho();
                    newKho.MaKho = txtMaKho.Text;
                    newKho.TenKho = txtTenKho.Text;
                    newKho.DiaChi= txtKhoiLuong.Text;                        
                    Kho_DB.InsertKho(newKho);
                    btnThem.Text = "Thêm";
                    frmQLKho_Load(sender, e);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi",MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtMaKho.Focus();                 
                }
            }
        }
        void ClearTextbox()
        {
            txtMaKho.Clear();
            txtTenKho.Clear();
            txtKhoiLuong.Clear();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            frmQLKho_Load(sender, e);
            btnThem.Text = "Thêm";
            btnSua.Text = "Sửa";
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult result = new DialogResult();
            string message = "Bạn có chắc chắn muốn xóa dữ liệu này ?" + "\n" + "Dữ liệu sau khi xóa sẽ không thể hồi phục !";
            result = MessageBox.Show(message, "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                Kho_DB.DeleteKho(txtMaKho.Text);
                MessageBox.Show("Xóa thành công !", "Thông báo", MessageBoxButtons.OK);
                frmQLKho_Load(sender, e);
            }
            
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "Sửa")
            {
                btnSua.Text = "Lưu";
                NotReadonlyTextbox();
                txtMaKho.ReadOnly = true; 
                txtTenKho.Focus();
            }
            else
            {
                try
                {
                    Kho newKho = new Kho();
                    newKho.MaKho = txtMaKho.Text;
                    newKho.TenKho = txtTenKho.Text;
                    newKho.DiaChi = txtKhoiLuong.Text;
                    Kho_DB.UpdateKho(txtMaKho.Text,newKho);
                    btnSua.Text = "Sửa";
                    frmQLKho_Load(sender, e);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtMaKho.Focus();
                }
            }
        }
    }
}
